package com.baomidou.springwind.controller.city;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.kisso.annotation.Permission;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.springwind.controller.BaseController;
import com.baomidou.springwind.entity.*;
import com.baomidou.springwind.enums.NodeTypeEnum;
import com.baomidou.springwind.pojo.DrivingRoute;
import com.baomidou.springwind.pojo.Test;
import com.baomidou.springwind.service.DepartmentsHangerService;
import com.baomidou.springwind.service.ProjectService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.DecimalFormat;
import java.util.*;

/**
 * <p>
 * 城市配送:订单分析
 * </p>
 *
 * @Author huangtao
 * @Date 2017-8-21
 */
@Controller
@RequestMapping("/network/city")
public class DepartmentsHangerController extends BaseController {

    @Autowired
    private DepartmentsHangerService departmentsHangerService;
    @Autowired
    private ProjectService projectService;
    /**
     * 城市数据导入--医院科室数据--显示医院
     */
    @RequestMapping("/department/list")
    @Permission("1001")
    public String customList(Model model) {
        EntityWrapper<Project> wrapper = new EntityWrapper<Project>();
        wrapper.eq("operator_id",getCurrentUserId());
        wrapper.in("state", Arrays.asList("1", "2"));
        model.addAttribute("prjList", projectService.selectList(wrapper));
        return pjaxPath("/file/department_hanger_list", model);
    }

    /**
     * 城市数据导入--客户分析数据--ajax获取数据
     */
    @ResponseBody
    @RequestMapping("/department/getData")
    @Permission("1001")
    public String getData(Page<DepartmentsHanger> page,String batchId) {
        page = new Page<DepartmentsHanger>(page.getCurrent(), page.getSize());
        EntityWrapper<DepartmentsHanger> wrapper = new EntityWrapper<DepartmentsHanger>();
        if (StringUtils.isNotEmpty(batchId)){
            wrapper.eq("batch_id",batchId);
        }
        wrapper.eq("operator_id", getCurrentUserId());
        wrapper.orderBy("create_time", false);
        page = departmentsHangerService.selectPage(page, wrapper);
        List<DepartmentsHanger> departmentsHangerList = page.getRecords();
        if (departmentsHangerList.size() > 0) {
            Project project = projectService.selectById(departmentsHangerList.get(0).getBatchId());
            for (DepartmentsHanger departmentsHanger : departmentsHangerList) {
                departmentsHanger.setBatchName(project == null ? "--" : project.getName());
            }
        }
        return jsonPage(page);
    }
    /**
     * 客户分析主界面
     */
    @RequestMapping("/department")
    @Permission("1001")
    public String customGroup(Model model) {
        return pjaxPath("/lpp/network/city/department/main", model);
    }
    private long MaxId(){
        EntityWrapper<DepartmentsHanger> wrapper1 = new EntityWrapper<DepartmentsHanger>();
        wrapper1.eq("batch_id", getProjectId());
        wrapper1.eq("operator_id", getCurrentUserId());
        wrapper1.orderBy("create_time", false);
        DepartmentsHanger departmentsHanger = departmentsHangerService.selectOne(wrapper1);
        return departmentsHanger.getId();
    }
    /**
     * 订单分析--各各科室占比分析分析
     */
    @ResponseBody
    @RequestMapping("/department/bar1")
    @Permission("1001")
    public String  flowBybarDepartments() throws Exception{
        String selectCount=request.getParameter("select");
        EntityWrapper<DepartmentsHanger> wrapper = new EntityWrapper<DepartmentsHanger>();
        if (!StringUtils.isBlank(selectCount)) {
            wrapper.eq("id", selectCount);
        }
        if(selectCount==null||selectCount.equals("")) {
            wrapper.eq("id", MaxId());
        }
        wrapper.eq("batch_id",getProjectId());
        wrapper.eq("operator_id", getCurrentUserId());
        final List<DepartmentsHanger> departmentsHangers = departmentsHangerService.selectList(wrapper);
        int wards=departmentsHangers.get(0).getWard();
        int outpatient=departmentsHangers.get(0).getOutpatient();
        int operate=departmentsHangers.get(0).getOperate();
        JSONObject ret = new JSONObject();
        //找到t需要的数据
        JSONArray t = new JSONArray();
        //找到a1需要的数据
        JSONObject a1 = new JSONObject();
        JSONObject a2 = new JSONObject();
        JSONObject a3 = new JSONObject();
        JSONArray jsonArray=new JSONArray();
        t.add("病房");
        t.add("门诊");
        t.add("手术室");
        a1.put("name","病房");
        a1.put("value",wards);
        jsonArray.add(a1);
        a2.put("name","门诊");
        a2.put("value",outpatient);
        jsonArray.add(a2);
        a3.put("name","手术室");
        a3.put("value",operate);
        jsonArray.add(a3);
        ret.put("x",t);
        ret.put("series1",jsonArray);
        return callbackSuccess(ret);
    }
}
